#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_ReadDTCInfomation_0x19_02_test.py
@Time    :   2025/04/07 13:31:28
@Author  :   YangLiang
@Version :   1.0
@Contact :   yangl@genvict.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from gettext import find
from pytest_assume.plugin import assume

import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

SID_0x19_reportDTCByStatusMask_noFault = [('19 02 09', '59 02 09'),
                                          ('19 02 02', '59 02 09'),
                                          ('19 02 04', '59 02 09'),
                                          ('19 02 08', '59 02 09'),
                                          ('19 02 10', '59 02 09'),
                                          ('19 02 0A', '59 02 09'),
                                          ('19 02 0B', '59 02 09'),
                                          ('19 02 0F', '59 02 09'),
                                          ('19 02 20', '59 02 09'),
                                          ('19 02 40', '59 02 09'),
                                          ('19 02 80', '59 02 09'),
                                          ('19 02 7F', '59 02 09'),
                                          ('19 02 FF', '59 02 09'),
                                       ]

class Test_reportDTCByStatusMask_Phy(object):
   '''NO Fault'''
   ids = ["Request {0}".format(req, resp) for req, resp in SID_0x19_reportDTCByStatusMask_noFault]
   @allure.severity('normal')
   @allure.title('物理寻址-默认会话-1902读取DTC测试')
   @allure.description('验证物理寻址默认会话下读取DTC')
   @pytest.mark.parametrize('req,resp', SID_0x19_reportDTCByStatusMask_noFault, ids=ids)
   def test_reportDTCByStatusMask_No_Fault_Default_Phy(self, action,  Driver, req, resp):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDS', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

   ids = ["Request {0}".format(req, resp) for req, resp in SID_0x19_reportDTCByStatusMask_noFault]
   @allure.severity('normal')
   @allure.title('物理寻址-扩展会话-1902读取DTC测试')
   @allure.description('验证物理寻址扩展会话下读取DTC')
   @pytest.mark.parametrize('req,resp', SID_0x19_reportDTCByStatusMask_noFault, ids=ids)
   def test_reportDTCByStatusMask_No_Fault_extended_Phy(self, action,  Driver, req, resp):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDS', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)


   # @allure.severity('normal')
   # @allure.story('Test read dtc information service 0x19')
   # @allure.description('Test reportDTCByStatusMask of 0x19 in default session with physical addressing')
   # 
   # @pytest.mark.repeat(10)
   # def test_reportDTCByStatusMask_No_Fault_Restart_withPowerOff_Phy(self, action,  Driver):
   #    iStep = 1
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 09", '59 02 09', 1, iStep, False, 'UDS', 'ClearFault')
   #    iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)
   #    iStep = Driver['CAN'].restartETC_Power(Driver,iStep)
   #    time.sleep(2)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 09", '59 02 09', 0, iStep, False, 'UDS', 'noClearFault')
   #    iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

   @allure.severity('normal')
   @allure.title('物理寻址-默认会话-1902读取DTC测试-设备重启验证')
   @allure.description('验证物理寻址默认会话下设备重启前后读取DTC')
   @pytest.mark.repeat(10)
   def test_reportDTCByStatusMask_No_Fault_Restart_with11_Phy(self, action,  Driver):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 09", '59 02 09', 1, iStep, False, 'UDS', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)
      iStep = Driver['CAN'].restartETC_11(Driver,iStep)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 09", '59 02 09', 0, iStep, False, 'UDS', 'noClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

   
   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in default session with physical addressing')
   
   def test_DTC_VBAT_HIGH_ByStatusMask_defaul_Phy(self, action,  Driver):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09', 1, iStep, False, 'UDS', 'ClearFault')
      time.sleep(1.5)
      # 设置电压为17V,并等待1.5s 制造 Overvoltage 故障,读取当前故障
      iStep = Driver['CAN'].SetVolt(Driver, 18.3, 3, iStep)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09 91 17 17 09', 0, iStep, False, 'UDS', 'noClearFault')

      # 设置电压为12V,并等待1.5s 恢复 Overvoltage 故障,读取当前故障
      iStep = Driver['CAN'].SetVolt(Driver, 12, 3, iStep)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09 91 17 17 08', 0, iStep, False, 'UDS', 'noClearFault')

   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in extended session with physical addressing')
   
   def test_DTC_VBAT_LOW_ByStatusMask_extended_Phy(self, action,  Driver):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09', 3, iStep, False, 'UDS', 'ClearFault')
      time.sleep(1.5)
      # 设置电压为8V,并等待1.5s 制造 Undervoltage 故障,读取当前故障 
      iStep = Driver['CAN'].SetVolt(Driver, 8.3, 3, iStep)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09 91 17 16 09', 0, iStep, False, 'UDS', 'noClearFault')

      # 设置电压为12V,并等待1.5s 恢复 Undervoltage 故障,读取当前故障
      iStep = Driver['CAN'].SetVolt(Driver, 12, 7, iStep)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09 91 17 16 08', 0, iStep, False, 'UDS', 'noClearFault')
   
   
   
   # @allure.severity('normal')
   # @allure.title('物理寻址-默认会话-1902读取DTC测试-产生故障后恢复')
   # @allure.description('验证物理寻址默认会话下产生故障后恢复')
   # def test_DTC_DLostCommunication_ByStatusMask_defaul_Phy(self, action,  Driver):
   #    iStep = 1
   #    time.sleep(1.5)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09', 1, iStep, False, 'UDS', 'ClearFault')
   #    iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

   #    # 暂停发送FZCU_BodyCAN_0x90信号,制造Lost Communication故障
   #    iStep = Driver['CAN'].SetLostCommunicationStatus(Driver, iStep, False)
   #    time.sleep(1)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09 D0 97 87 09', 0, iStep, False, 'UDS', 'noClearFault')

   #    # 恢复发送FZCU_BodyCAN_0x90信号,恢复Lost Communication故障
   #    iStep = Driver['CAN'].SetLostCommunicationStatus(Driver, iStep, True)
   #    time.sleep(1)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 08', '59 02 09 D0 97 87 08', 0, iStep, False, 'UDS', 'noClearFault')
   #    time.sleep(5)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 08', '59 02 09', 0, iStep, False, 'UDS', 'noClearFault')
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '14 FF FF FF', '54', 0, iStep, False, 'UDSFun', 'noClearFault')
   #    time.sleep(2)


   # @allure.severity('normal')
   # @allure.title('物理寻址-扩展会话-1902读取DTC测试-产生故障后恢复')
   # @allure.description('验证物理寻址扩展会话下产生故障后恢复')
   # def test_DTC_DLostCommunication_ByStatusMask_Extended_Phy(self, action,  Driver):
   #    iStep = 1
   #    time.sleep(1.5)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09', 3, iStep, False, 'UDS', 'ClearFault')
   #    iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

   #    # 暂停发送FZCU_BodyCAN_0x90信号,制造Lost Communication故障
   #    iStep = Driver['CAN'].SetLostCommunicationStatus(Driver, iStep, False)
   #    time.sleep(1)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09 D0 97 87 09', 0, iStep, False, 'UDS', 'noClearFault')

   #    # 恢复发送FZCU_BodyCAN_0x90信号,恢复Lost Communication故障
   #    iStep = Driver['CAN'].SetLostCommunicationStatus(Driver, iStep, True)
   #    time.sleep(1)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 08', '59 02 09 D0 97 87 08', 0, iStep, False, 'UDS', 'noClearFault')
   #    logging.info('wait 5s')
   #    time.sleep(5)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 08', '59 02 09', 0, iStep, False, 'UDS', 'noClearFault')
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '14 FF FF FF', '54', 0, iStep, False, 'UDSFun', 'noClearFault')
   #    time.sleep(2)


class Test_reportDTCByStatusMask_Func(object):

   '''NO Fault'''
   ids = ["Request {0}".format(req, resp) for req, resp in SID_0x19_reportDTCByStatusMask_noFault]
   @allure.severity('normal')
   @allure.title('功能寻址-默认会话-1902读取DTC测试')
   @allure.description('验证功能寻址默认会话下读取DTC')
   @pytest.mark.parametrize('req,resp', SID_0x19_reportDTCByStatusMask_noFault, ids=ids)
   def test_reportDTCByStatusMask_No_Fault_Default_Func(self, action,  Driver, req, resp):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

   ids = ["Request {0}".format(req, resp) for req, resp in SID_0x19_reportDTCByStatusMask_noFault]
   @allure.severity('normal')
   @allure.title('功能寻址-扩展会话-1902读取DTC测试')
   @allure.description('验证功能寻址扩展会话下读取DTC')
   @pytest.mark.parametrize('req,resp', SID_0x19_reportDTCByStatusMask_noFault, ids=ids)
   def test_reportDTCByStatusMask_No_Fault_extended_Func(self, action,  Driver, req, resp):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)


   # @allure.severity('normal')
   # @allure.story('Test read dtc information service 0x19')
   # @allure.description('Test reportDTCByStatusMask of 0x19 in default session with functional addressing')
   # 
   # @pytest.mark.repeat(10)
   # def test_reportDTCByStatusMask_No_Fault_Restart_withPowerOff_Func(self, action,  Driver):
   #    iStep = 1
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 09", '59 02 09', 1, iStep, False, 'UDSFun', 'ClearFault')
   #    iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)
   #    iStep = Driver['CAN'].restartETC_Power(Driver,iStep)
   #    time.sleep(2)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 09", '59 02 09', 0, iStep, False, 'UDSFun', 'noClearFault')
   #    iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

   @allure.severity('normal')
   @allure.title('功能寻址-默认会话-1902读取DTC测试-设备重启验证')
   @allure.description('验证功能寻址默认会话下设备重启前后读取DTC')
   @pytest.mark.repeat(10)
   def test_reportDTCByStatusMask_No_Fault_Restart_with11_Func(self, action,  Driver):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 09", '59 02 09', 1, iStep, False, 'UDSFun', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)
      iStep = Driver['CAN'].restartETC_11(Driver,iStep)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 09", '59 02 09', 0, iStep, False, 'UDSFun', 'noClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

   
   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in default session with functional addressing')
   
   def test_DTC_VBAT_HIGH_ByStatusMask_defaul_Func(self, action,  Driver):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09', 1, iStep, False, 'UDSFun', 'ClearFault')
      time.sleep(1.5)
      # 设置电压为17V,并等待1.5s 制造 Overvoltage 故障,读取当前故障
      iStep = Driver['CAN'].SetVolt(Driver, 18.3, 3, iStep)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09 91 17 17 09', 0, iStep, False, 'UDSFun', 'noClearFault')

      # 设置电压为12V,并等待1.5s 恢复 Overvoltage 故障,读取当前故障
      iStep = Driver['CAN'].SetVolt(Driver, 12, 3, iStep)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09 91 17 17 08', 0, iStep, False, 'UDSFun', 'noClearFault')

   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in extended session with functional addressing')
   
   def test_DTC_VBAT_LOW_ByStatusMask_extended_Func(self, action,  Driver):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09', 3, iStep, False, 'UDSFun', 'ClearFault')
      time.sleep(1.5)
      # 设置电压为8V,并等待1.5s 制造 Undervoltage 故障,读取当前故障 
      iStep = Driver['CAN'].SetVolt(Driver, 8.3, 3, iStep)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09 91 17 16 09', 0, iStep, False, 'UDSFun', 'noClearFault')

      # 设置电压为12V,并等待1.5s 恢复 Undervoltage 故障,读取当前故障
      iStep = Driver['CAN'].SetVolt(Driver, 12, 7, iStep)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09 91 17 16 08', 0, iStep, False, 'UDSFun', 'noClearFault')
   
   
   
   # @allure.severity('normal')
   # @allure.title('功能寻址-默认会话-1902读取DTC测试-产生故障后恢复')
   # @allure.description('验证功能寻址默认会话下产生故障后恢复')
   # def test_DTC_DLostCommunication_ByStatusMask_defaul_Func(self, action,  Driver):
   #    iStep = 1
   #    time.sleep(1.5)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09', 1, iStep, False, 'UDSFun', 'ClearFault')
   #    iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

   #    # 暂停发送FZCU_BodyCAN_0x90信号,制造Lost Communication故障
   #    iStep = Driver['CAN'].SetLostCommunicationStatus(Driver, iStep, False)
   #    time.sleep(1)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09 D0 97 87 09', 0, iStep, False, 'UDSFun', 'noClearFault')

   #    # 恢复发送FZCU_BodyCAN_0x90信号,恢复Lost Communication故障
   #    iStep = Driver['CAN'].SetLostCommunicationStatus(Driver, iStep, True)
   #    time.sleep(1)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 08', '59 02 09 D0 97 87 08', 0, iStep, False, 'UDSFun', 'noClearFault')
   #    time.sleep(5)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 08', '59 02 09', 0, iStep, False, 'UDSFun', 'noClearFault')
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '14 FF FF FF', '54', 0, iStep, False, 'UDSFun', 'noClearFault')
   #    time.sleep(2)


   # @allure.severity('normal')
   # @allure.title('功能寻址-扩展会话-1902读取DTC测试-产生故障后恢复')
   # @allure.description('验证功能寻址扩展会话下产生故障后恢复')
   # def test_DTC_DLostCommunication_ByStatusMask_Extended_Func(self, action,  Driver):
   #    iStep = 1
   #    time.sleep(1.5)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09', 3, iStep, False, 'UDSFun', 'ClearFault')
   #    iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

   #    # 暂停发送FZCU_BodyCAN_0x90信号,制造Lost Communication故障
   #    iStep = Driver['CAN'].SetLostCommunicationStatus(Driver, iStep, False)
   #    time.sleep(1)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09 D0 97 87 09', 0, iStep, False, 'UDSFun', 'noClearFault')

   #    # 恢复发送FZCU_BodyCAN_0x90信号,恢复Lost Communication故障
   #    iStep = Driver['CAN'].SetLostCommunicationStatus(Driver, iStep, True)
   #    time.sleep(1)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 08', '59 02 09 D0 97 87 08', 0, iStep, False, 'UDSFun', 'noClearFault')
   #    logging.info('wait 5s')
   #    time.sleep(5)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '14 FF FF FF', '54', 0, iStep, False, 'UDSFun', 'noClearFault')
   #    time.sleep(2)
